// pages/options/options.js
var util = require('../../../utils/util.js')
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bg1:'white',
    bg2: 'white',
    bg3: 'white',
    bgdata:0,
    date:'',
    time:'',
    ifName:true,
    phone:'',
    color1: 'gainsboro',
    color2: 'gainsboro',
    color3: 'gainsboro',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var TIME = util.formatTime(new Date());

    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    this.setData({
      date:Y+'-'+M+'-'+D,
      time: TIME
    })


  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  btn_1:function(){
      this.setData({
        bg1: '#01B6B1',
        bg2: 'white',
        bg3: 'white',
        bgdata: 1,
        color1:'white',
        color2: 'gainsboro',
        color3: 'gainsboro',
      })
  },
  btn_2: function () {
    this.setData({
      bg1: 'white',
      bg2: '#01B6B1',
      bg3: 'white',
      bgdata: 2,
      color2: 'white',
      color1: 'gainsboro',
      color3: 'gainsboro',
    })
  },
  btn_3: function () {
    this.setData({
      bg1: 'white',
      bg2: 'white',
      bg3: '#01B6B1',
      color3: 'white',
      color2: 'gainsboro',
      color1: 'gainsboro',
      bgdata: 3
    })
  },
  bindDateChange:function(e){
    this.setData({
      date:e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  bindInputPhone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  textareaInput:function(res){
    var that = this
    that.setData({
      textarea:res.detail.value
    })
  },
  bindInputPhone:function(res){
    var that = this
    that.setData({
      phoneNumber: res.detail.value
    })
  },
  submit:function(){
    var that = this
    if (that.data.bgdata == 0){
      wx.showModal({
        title: '提示',
        content: '请选择具体问题',
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
          
        }
      })
      return
    }
    if (that.data.textarea == '' || that.data.textarea == undefined){
      wx.showModal({
        title: '提示',
        content: '请填写问题描述',
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
          
        }
      })
      return
    }
    if (that.data.phoneNumber == '' || that.data.phoneNumber == undefined){
      wx.showModal({
        title: '提示',
        content: '请填写联系方式',
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
          return
        }
      })
      return
    }
    
    var bgdata ='';
    if(that.data.bgdata == 1){
      bgdata = '视频课程'
    }else if(that.data.bgdata == 2){
      bgdata = '功能流程'
    }else{
      bgdata = '意见建议'
    }
    wx.request({
      url: base_url+'/setSuggestion/',
      method: 'POST',
      data: {
        textarea: that.data.textarea,
        phoneNumber: that.data.phoneNumber,
        datetime: that.data.date + ' ' + that.data.time,
        bgdata: bgdata
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if(res.data['flag'] == true){
          wx.showModal({
            title: '提示',
            content: '提交成功',
            showCancel: false,
            confirmText: '确定',
            success: function (res) {
              that.setData({
                phone:'',
                question:''
              })
            }
          })
        }
      }
    })
  }
})